﻿using GodSystemBLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormCateUpdate : Form
    {
        private static FormCateUpdate formCateUpdate = null;
        private static int id = 0;
        private static string originNameCate = null;
        public FormCateUpdate()
        {
            InitializeComponent();
            InitData();
        }
        private void InitData()
        {
           textBox_CateName.Text = FormProductCate.categoriesModel.CateName;
           originNameCate = FormProductCate.categoriesModel.CateName;
           id = FormProductCate.categoriesModel.Id; 
        }

        public static FormCateUpdate CreateInstance()
        {
            if (formCateUpdate == null || formCateUpdate.IsDisposed)
            {
                formCateUpdate = new FormCateUpdate();
            }
            return formCateUpdate;
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void button_Submit_Click(object sender, EventArgs e)
        {
            string cateName = textBox_CateName.Text.Trim();
            if (string.IsNullOrEmpty(cateName))
            {
                MessageBox.Show("分类名称不能为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            if (originNameCate.Equals(cateName))
            {
                MessageBox.Show("请修改为不一致的名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            CateBLL cateBLL = new CateBLL();
            object num = cateBLL.CheckCateName(cateName);
            if(Convert.ToInt32(num) > 0)
            {
                MessageBox.Show("分类名称已存在，请修改为别的名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int result = cateBLL.UpdateCateName(cateName, id);
            if(result > 0)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("分类名称修改失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
